package cn.saltice.idempotence.idem.common;

/**
 * @program: idempotence
 * @description:
 * @author: Mr.Liu
 * @create: 2020-10-07 22:14
 **/
public class ContextHolder {
    private static final ThreadLocal<RequestContext> contextHolder = new ThreadLocal<RequestContext>(){
        @Override
        protected RequestContext initialValue() {
            return new DefaultContext();
        }
    };

    /**
     * 获取当前线程的上下文
     * @return
     */
    public static RequestContext getRequestContext(){
        return contextHolder.get();
    }

    /**
     * 为当前线程设置上下文
     * @param requestContext
     */
    public static void setRequestContext(RequestContext requestContext){
        contextHolder.set(requestContext);
    }

    /**
     * 清理当前线程的上下文
     */
    public static void clearCurrentContext(){
        contextHolder.remove();
    }
}
